﻿using open_api_sdk_csharp.com.chinaums.open.api.constants;
using open_api_sdk_csharp.response;
using System.Net;
using System.Text;
using Jayrock.Json.Conversion;
using System;
/**
通讯基类
**/
namespace open_api_sdk_csharp.com.chinaums.open.api.internals.utils.http
{
    public sealed class HttpTransport : IHttpTransport
    {
        private static volatile HttpTransport instance;
        private static readonly object obj = new object();
        private HttpTransport() { }
        public static HttpTransport Instance
        {
            get
            {
                if (null == instance)
                {
                    lock (obj)
                    {
                        if (null == instance)
                        {
                            instance = new HttpTransport();
                        }
                    }

                }
                return instance;
            }
        }
        public string doPost(OpenApiContext context, string request)
        {
            OpenModelParser parser = new OpenModelParser();
            string url = context.ApiServiceUrl;
            ConfigBean configBean = context.ConfigBean;
            context.Request = request;
            if (!configBean.IsProd) {
                OpenApiLogger.logInfo("process context:" + JsonConvert.ExportToString(context));
                OpenApiLogger.logInfo("requst params:" + request);
            }
            WebClient webClient = new WebClient();
            Encoding encoding = Encoding.UTF8;
            string result = string.Empty;
            webClient.Headers.Add("Content-Type", "application/json");
            TokenResponse currentToken = context.CurrentToken;
            webClient.Headers.Add("Authorization", "OPEN-ACCESS-TOKEN AccessToken=" + currentToken.accessToken);
            byte[] postData = encoding.GetBytes(request);
            byte[] responseData = webClient.UploadData(url, "POST", postData);
            string responseContent = encoding.GetString(responseData);
            OpenApiLogger.logInfo("response msg:" + responseContent);
            context.Response = responseContent;
            return responseContent;
        }
        public  string doPost(bool isDebug, string url, string token, string request)
        {
            try
            {
                if (!isDebug)
                {
                    OpenApiLogger.logInfo("url:" + url);
                    OpenApiLogger.logInfo("requst params:" + request);
                }
                WebClient webClient = new WebClient();
                Encoding  encoding = Encoding.UTF8;
                string result = string.Empty;
                webClient.Headers.Add("Content-Type", "application/json");
                request = request.Replace("'", "\"");
                byte[] postData = encoding.GetBytes(request);
                if (!string.IsNullOrEmpty(token))
                {
                    webClient.Headers.Add("Authorization", "OPEN-ACCESS-TOKEN AccessToken="+token);
                }
                byte[] responseData = webClient.UploadData(url, "POST", postData);
                string responseContent = encoding.GetString(responseData);
                OpenApiLogger.logInfo("response msg:" + responseContent);
                return responseContent;
            }
            catch (Exception e)
            {
                OpenApiLogger.logError(e.StackTrace + "{}:\n"+e.Message);
            }
            return null;
        }
    }
}
